package com.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChessGame {

    private Integer[] chess;
    private String[] chessUser;

    private Integer[] chessBoard;

    private Map<Integer, Integer[]> db;

    private Integer[] dbCount;

    private Integer nowmove;



    public ChessGame(){
        dbCount = new Integer[]{10, 10};

        nowmove = 0;

        chess = new Integer[2];
        chess[0] = 76;
        chess[1] = 4;

        chessUser = new String[2];
        chessUser[0] = null;
        chessUser[1] = null;

        chessBoard = new Integer[81];
        for (int i = 0; i < 81; i++) {
            chessBoard[i] = 0;

        }
        chessBoard[4] = 2;
        chessBoard[76] = 1;

        db = new HashMap<Integer, Integer[]>();
        Integer[] db0 = new Integer[81];
        Integer[] db90 = new Integer[81];
        for (int i = 0; i < 81; i++) {
            db0[i] = 0;
            db90[i] = 0;
        }
        db.put(0, db0);
        db.put(90, db90);
    }




    //棋子控制
    public Integer getEmptyeChess(){
        Integer ret = null;
        if(chessUser[0] == null) {
            ret = 0;
        } else if(chessUser[1] == null){
            ret = 1;
        }
        return ret;
    }

    public void giveUp(String user){
        if(chessUser[0].equals(user)) {
            chessUser[0] = null;
        } else if(chessUser[1].equals(user)){
            chessUser[1] = null;
        }
    }

    public void giveUpAll(){
        chessUser[0] = null;
        chessUser[1] = null;
    }

    public void login(Integer id, String user){
        chessUser[id] = user;
    }

    public void chessSet(Integer id, Integer place){
        chess[id] = place;
    }

    public Integer getPlace(Integer id){
        return chess[id];
    }

    //棋盘部分
    public List<Map<String, Integer>> selectColor(){
        List<Map<String, Integer>> ret = null;

        for (int i = 0; i < 81; i++) {
            if(chessBoard[i] != 0){
                if(ret == null){
                    ret = new ArrayList<Map<String, Integer>>();
                }
                Map<String, Integer> j = new HashMap<String, Integer>();
                j.put("id",i);
                j.put("color", chessBoard[i]);
                ret.add(j);
            }
        }
        return ret;
    }

    public void paint(Integer id, Integer color){
        chessBoard[id] = color;
    }

    public void aClear(){
        for (int i = 0; i < 81; i++) {
            chessBoard[i] = 0;
        }
    }

    //挡板部分
    public void dbSet(Integer id, Integer rotate){
        db.get(rotate)[id] = 1;

    }

    public List<Map<String, Integer>> getDb(){
        List<Map<String, Integer>> ret = null;
        Integer[] db0 = db.get(0);
        Integer[] db90 = db.get(90);
        for (int i = 0; i < 81; i++) {
            if(db0[i] != 0){
                if(ret == null){
                    ret = new ArrayList<Map<String, Integer>>();
                }
                Map<String, Integer> j = new HashMap<String, Integer>();
                j.put("id", i);
                j.put("rotate", 0);
                ret.add(j);
            }
            if(db90[i] != 0){
                if(ret == null){
                    ret = new ArrayList<Map<String, Integer>>();
                }
                Map<String, Integer> j = new HashMap<String, Integer>();
                j.put("id", i);
                j.put("rotate", 90);
                ret.add(j);
            }
        }
        return ret;
    }

    public Integer empty(Integer id, Integer rotate){
        return db.get(rotate)[id];
    }

    public void bClear(){
        Integer[] db0 = db.get(0);
        Integer[] db90 = db.get(90);
        for (int i = 0; i < 81; i++) {
            db0[i] = 0;
            db90[i] = 0;
        }
    }




    public Integer getNowmove() {
        return nowmove;
    }

    public void setNowmove(Integer nowmove) {
        this.nowmove = nowmove;
    }

    public Integer getDbCount(Integer index) {
        return dbCount[index];
    }

    public void setDbCount(Integer index, Integer dbCount) {
        this.dbCount[index] = dbCount;
    }
}
